web/src/pages/[category].astro
114 lines · 2812 bytes
1---
2import Layout from '@layouts/Layout.astro';
3import SectionList from '@components/things/SectionList.astro';
4import Main from '@components/scafold/MainCard.astro';
5
6import { fetchData, slugify } from '@utils/fetch-data';
7import { truncate } from '@utils/meta-tags';
8
9import type { Section } from '../types/Service';
10import Button from '@components/form/Button.astro';
11
12interface Props {
13 title: string;
14 sections: Section[];
15}
16
17const { title, sections } = Astro.props;
18
19const categories = (await fetchData()).categories;
20
21export async function getStaticPaths() {
22 const pages = await fetchData().then((data) => {
23 return data.categories.map((category) => {
24 return {
25 category: slugify(category.name),
26 title: category.name,
27 sections: category.sections,
28 };
29 });
30 });
31
32 return pages.map(({ category, title, sections }) => {
33 return {
34 params: { category },
35 props: { title, sections },
36 };
37 });
38}
39
40const makeCarasolData = () => {
41 return {
42 '@context': 'https://schema.org',
43 '@type': 'ItemList',
44 itemListElement: sections.map((section, index) => ({
45 '@type': 'ListItem',
46 position: index + 1,
47 name: section.name,
48 url: `https://awesome-privacy.xyz/${slugify(title)}/${slugify(section.name)}/`,
49 })),
50 };
51};
52
53const makeBreadcrumbs = () => {
54 return [
55 { name: 'Home', item: '/' },
56 { name: title, item: slugify(title) },
57 ];
58};
59
60const makeDescriptionTag = () =>
61 truncate(
62 `Privacy-respecting ${title.toLowerCase()}: ` +
63 `${sections.map((section) => section.name).join(', ')}. ` +
64 `Compare free, open source and secure alternatives.`,
65 );
66
67const makePaginationLinks = () => {
68 const index = categories.findIndex((category) => category.name === title);
69 const previousCategory = index > 0 ? categories[index - 1].name : null;
70 const nextCategory =
71 index < categories.length - 1 ? categories[index + 1].name : null;
72 return { previous: previousCategory, next: nextCategory };
73};
74
75const { previous, next } = makePaginationLinks();
76---
77
78<Layout
79 title={`${title} | Awesome Privacy`}
80 description={makeDescriptionTag()}
81 breadcrumbs={makeBreadcrumbs()}
82 customSchemaJson={makeCarasolData()}
83>
84 <Main>
85 <div class="breadcrumbs">
86 <span>
87 <a href="/">Awesome Privacy</a>
88 ➔ <a href={`/${slugify(title)}/`}>{title}</a>
89 </span>
90 </div>
91 <SectionList title={title} sections={sections} bigTitle={true} />
92 Or, <a href={`/all/#${slugify(title)}`}>Browse All {title}</a>
93 <div class="pagination-navigation">
94 {
95 previous ? (
96 <Button url={`/${slugify(previous)}/`}>
97 <span>← Previous</span>
98 <p>{previous}</p>
99 </Button>
100 ) : (
101 <p class="nothing" />
102 )
103 }
104 {
105 next && (
106 <Button url={`/${slugify(next)}/`}>
107 <span>Next →</span>
108 <p>{next}</p>
109 </Button>
110 )
111 }
112 </div>
113 </Main>
114</Layout>